<?php 

require('../qcubed.inc.php');

require('panels/OrgOrgPanel.class.php');
require('panels/OrgWorkPanel.class.php');
require('panels/OrgAddressPanel.class.php');
require('panels/OrgPersonPanel.class.php');
require('panels/OrgInfoPanel.class.php');
//0122 118 581

class OrgForm extends QForm {
    // Local instance of the PersonMetaControl
    protected $mctOrg;
    protected $mctAddress;
    protected $objAddress;
    protected $objOrg;
    //Data Grid
    protected $dtgPeople;
    //Tabs
    protected $tabMain;
    //Panels
    protected $pnlSecOrg;
    protected $pnlSecWork;
    protected $pnlSecAddress;
    protected $pnlSecPerson;
    protected $pnlSecInfo;

    protected $btnSave;
    protected $btnCancel;

    protected $authSys;

    protected function Form_Run() {
        $this->authSys = new Perm();
        $this->authSys->IsAuthenticated(true);
    }

    protected function Form_Create() {
        //Create Meta Data Controlers
        $this->mctOrg = OrgMetaControl::CreateFromPathInfo($this);
        $this->objOrg = $this->mctOrg->__get("Org");

        //Check  and dont aloow if no permition
        if ( $this->mctOrg->EditMode)
            $this->authSys->Check_Org_Perm(  $this->objOrg->Id,true );

        if ( $this->objOrg->AddressId)
            $this->mctAddress = AddressMetaControl::Create($this, $this->objOrg->AddressId, QMetaControlCreateType::EditOnly);
        else
            $this->mctAddress = AddressMetaControl::Create($this, null);

        $this->tabMain = new Jtab("MainTab");

        $this->pnlSecOrg = new QePanel($this);
        $this->pnlSecOrg = new OrgOrgPanel($this->pnlSecOrg, $this->mctOrg);
        $this->tabMain->AddTab(QApplication::Translate("Organization") , $this->pnlSecOrg);


        $this->pnlSecWork = new QePanel($this);
        $this->pnlSecWork = new OrgWorkPanel($this->pnlSecWork, $this->mctAddress);
        $this->tabMain->AddTab(QApplication::Translate("Work") , $this->pnlSecWork);


        $this->pnlSecAddress = new QePanel($this);
        $this->pnlSecAddress = new OrgAddressPanel($this->pnlSecAddress, $this->mctAddress);
        $this->tabMain->AddTab(QApplication::Translate("Address") , $this->pnlSecAddress);

        $bolEdit = $this->mctAddress->__get("EditMode");

        if ($bolEdit) {
            $this->pnlSecPerson = new QePanel($this);
            //Create and assign new panel
            $this->pnlSecPerson = new OrgPersonPanel($this->pnlSecPerson, $this->objOrg);
            //Add tab
            $this->tabMain->AddTab(QApplication::Translate("Person(s)") , $this->pnlSecPerson);

        }

        $this->pnlSecInfo = new QePanel($this);
        $this->pnlSecInfo = new OrgInfoPanel($this->pnlSecInfo, $this->mctOrg, $this->mctAddress) ;
        $this->tabMain->AddTab(QApplication::Translate("Record Info") , $this->pnlSecInfo);

        $this->btnSave = new QButton($this);
        $this->btnSave->Text = QApplication::Translate('Save');
        $this->btnSave->AddAction(new QClickEvent(), new QAjaxAction('btnSave_Click'));
        $this->btnSave->CausesValidation = true;

        $this->btnCancel = new QButton($this);
        $this->btnCancel->Text = QApplication::Translate('Cancel');
        $this->btnCancel->AddAction(new QClickEvent(), new QAjaxAction('btnCancel_Click'));

    }

    protected function Form_Validate() {
        $blnToReturn = true;
        $blnFocused = false;

        if ( $this->pnlSecOrg->Validate() ) {
            $blnToReturn=false;
            if (!$blnFocused) {
                $this->tabMain->ChangeTabById(0);
                $blnFocused=true;
            }
        }
        if ( $this->pnlSecWork->Validate() ) {
            $blnToReturn=false;
            if (!$blnFocused) {
                $this->tabMain->ChangeTabById(1);
                $blnFocused=true;
            }
        }

        /*
                if ( $this->pnlSecAddress->Validate() ){
                    $blnToReturn=false;
                    if (!$blnFocused){ $this->tabMain->ChangeTabById(3);$blnFocused=true;}
                }
        */
        //Change Tab
        return $blnToReturn;
    }
    // Button Event Handlers
    protected function btnSave_Click($strFormId, $strControlId, $strParameter) {
        $this->mctAddress->SaveAddress();
        $this->mctOrg->SaveOrg();

        $this->objAddress = $this->mctAddress->__get("Address");
        $this->objAddress->AssociateOrg ($this->objOrg);
        $this->objAddress->Save();
        $this->RedirectToListPage();
    }
    protected function btnCancel_Click($strFormId, $strControlId, $strParameter) {
        $this->RedirectToListPage();
    }

    // Other Methods
    protected function RedirectToListPage() {
        QApplication::Redirect(__ORG__ . '/Organization_list.php');

    }

}
OrgForm::Run('OrgForm','tpl/organization_edit.tpl.php');
?>
